from web import models
from django import forms


class BaseForm(object):
    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        for name,item in self.fields.items():
            old_class=item.widget.attrs.get('class','')
            item.widget.attrs['class']=f'{old_class} form-control'
            item.widget.attrs['placeholder']=f'请输入{item.label}'

class IssueForm(BaseForm,forms.ModelForm):
    class Meta:
        model=models.Issues
        exclude = ['project','creator','create_datetime','latest_update_datetime']
        widgets={
            'assign':forms.Select(attrs={'class':'selectpicker','data-live-search':'true'}),
            'attention':forms.SelectMultiple(attrs={'class': 'selectpicker','data-live-search':'true','data-actions-box':'true'}),
            'parent': forms.SelectMultiple(
                attrs={'class': 'selectpicker', 'data-live-search': 'true', 'data-actions-box': 'true'})
        }

    def __init__(self,request,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.request = request
        #处理问题类型
        total_list=[('','没有选中任何项'),]
        dataset=models.IssuesType.objects.filter(project=self.request.tracer.project).values_list('id','title')
        total_list.extend(dataset)
        self.fields['issues_type'].choices=total_list
        #处理问题模块
        module_list=[('','没有选中任何项'),]
        moduleset=models.Module.objects.filter(project=self.request.tracer.project).values_list('id','title')
        module_list.extend(moduleset)
        self.fields['module'].choices=module_list
        #处理指派和跟随
        user_list=[('','没有选中任何项'),(request.tracer.project.creator_id,request.tracer.project.creator.username),]
        project_user_list=models.ProjectUser.objects.filter(project=self.request.tracer.project).values_list('user_id','user__username')
        user_list.extend(project_user_list)
        self.fields['assign'].choices=user_list
        self.fields['attention'].choices=user_list
        #处理父问题
        issues_list=[('','没有选中任何项'),]
        issuesset=models.Issues.objects.filter(project=self.request.tracer.project).values_list('id','subject')
        issues_list.extend(issuesset)
        self.fields['parent'].choices=issues_list
        # print(self.fields['parent'].widget.attrs)

class IssueReplyForm(forms.ModelForm):
    class Meta:
        model=models.IssuesReply
        fields = ['content','reply']